function [Out,SubInt] = myIntFunc(FuncHandle, A,B, Method,NumSubInt)
% myIntFunc Calculates the numerical integral of the input function 
% 
% This function uses the MidPoint, Trapezoid or Simpson's rule method
% to calculate an integral using 20 sub-intervals. 
%
% Function call: [Out] = HW3_myIntFunc(FuncHandle, A,B, Method)
%   INPUTS
% FuncHandle:  A function handle to the function to be integrated
% A: Lower limit of integral
% B: Upper limit of integral
% Method: A string to select method. 'MidPoint', 'Trapezoid' or 'Simpson'
% NubSubInt: Number of sub intervals
%   OUTPUT
% Out: The calculated integral
% SubInt: Sub interval size

N=NumSubInt; %Number of sub-intervals 

%This is a switch loop to select the method
    switch Method 
        %If the variable method is contained the string 'MidPoint' then
        %this case will be implemented.
        case 'MidPoint'

             Space = (B-A)/N; %the length of intervals
             Starts = linspace(A,B-Space,N); % the beinning of intervals
             Mids = Starts + Space/2; % the end of intevals
             
             MidsHight = FuncHandle(Mids); %Height of function
             
             Out = 0;
             for ii = 1:N
                
                 Out = Out+MidsHight(ii)*Space; %sum of the areas of rectangles
                 
             end
             
             %If the variable method is contained the string 'Trapezoid' then
             %this case will be implemented.
        case 'Trapezoid' 

             Space = (B-A)/N;
             Starts = linspace(A,B-Space,N);
             Ends = linspace(A+Space,B,N);
             Leng = FuncHandle(Starts) + FuncHandle(Ends); %hiht of the start points + hight of the end points
             
             Out = 0;
             for ii = 1:N
                
                 Out = Out + 0.5*Leng(ii)*Space; % sum of the areas of the trapezoids
                 
             end
             
             
            %If the variable method is contained the string 'Simpson' then
            %this case will be implemented.
        case 'Simpson'

             Space = (B-A)/N;
             Starts = linspace(A,B-Space,N);
             Ends = linspace(A+Space,B,N); 
             Mids = (Starts + Ends)/2;
             
             Out = 0;
             for ii = 1:N
                
                FatStart=FuncHandle(Starts(ii));
                FatMid=FuncHandle(Mids(ii));
                FatEnd=FuncHandle(Ends(ii));
                
                %sum of the areas under the polynomial:
                Update=(Ends(ii)-Starts(ii))/6*(FatStart+4*FatMid+FatEnd);
                Out = Out + Update;
                
             end
    end
    
    SubInt=Space;
    

end